package oracle.springbootxm.controller;

import oracle.springbootxm.pojo.GoodsInfo;
import oracle.springbootxm.pojo.UserInfo;
import oracle.springbootxm.service.GoodsService;
import oracle.springbootxm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/userlogin")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            UserInfo userInfo = userService.queryUserInfoByName(username);
            if (userInfo != null && userInfo.getPassword().equals(password)) {
                if (userInfo.getUserpower() == 0){
                    session.setAttribute("userInfo", userInfo);
                    return "/jump";
                }else {
                    session.setAttribute("superuser", userInfo);
                    return "/addgoods";
                }
            }
        }
        return "login";
    }
}
